<template>
  <a-drawer width="35%" :label-col="4" :wrapper-col="14" :visible="open" @close="onClose">
    <a-divider orientation="left">
      <b>{{ formTitle }}</b>
    </a-divider>
    <a-form-model ref="form" :model="form" :rules="rules">
      <a-form-model-item label="校区编号" prop="deptId" >
        <a-tree-select
              v-model="form.deptId"
              style="width: 100%"
              :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
              :tree-data="treeData"
              placeholder="请选择校区编号"
              :replaceFields="{children:'children', title:'label', key:'id', value: 'id' }	"
              tree-default-expand-all
            >
          </a-tree-select>
      </a-form-model-item>
      <a-form-model-item label="姓名" prop="name" >
        <a-input v-model="form.name" placeholder="请输入姓名" />
      </a-form-model-item>
      <a-form-model-item label="角色" prop="roleType" >
        <a-radio-group v-model="form.roleType" button-style="solid">
          <a-radio-button v-for="(d, index) in roleTypeOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-radio-button>
        </a-radio-group>
      </a-form-model-item>
      <a-form-model-item label="职级" prop="rankType" >
        <a-radio-group v-model="form.rankType" button-style="solid">
          <a-radio-button v-for="(d, index) in rankTypeOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-radio-button>
        </a-radio-group>
      </a-form-model-item>
      <a-form-model-item label="性别" prop="sex" >
        <a-radio-group v-model="form.sex" button-style="solid">
          <a-radio-button v-for="(d, index) in sexOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-radio-button>
        </a-radio-group>
      </a-form-model-item>
      <a-form-model-item v-if="form.roleType == 1" label="班型" prop="classType" >
        <a-checkbox-group v-model="classType" >
          <a-checkbox v-for="(d, index) in classTypeOptions" :key="index" :value="d.dictValue">
            {{ d.dictLabel }}
          </a-checkbox>
        </a-checkbox-group>
      </a-form-model-item>
      <a-form-model-item label="电话" prop="phone" >
        <a-input v-model="form.phone" placeholder="请输入电话" />
      </a-form-model-item>
      <a-form-model-item label="备注" prop="remark" >
        <a-input v-model="form.remark" placeholder="请输入内容" type="textarea" allow-clear />
      </a-form-model-item>
      <div class="bottom-control">
        <a-space>
          <a-button type="primary" :loading="submitLoading" @click="submitForm">
            保存
          </a-button>
          <a-button type="dashed" @click="cancel">
            取消
          </a-button>
        </a-space>
      </div>
    </a-form-model>
  </a-drawer>
</template>

<script>
import { getManagerUser, addManagerUser, updateManagerUser } from '@/api/manager/managerUser'
import { throws } from 'assert'
import Q from 'q'

export default {
  name: 'CreateForm',
  props: {
    roleTypeOptions: {
      type: Array,
      required: true
    },
    rankTypeOptions: {
      type: Array,
      required: true
    },
    sexOptions: {
      type: Array,
      required: true
    },
    treeData: {
      type: Array,
      required: true
    }
  },
  components: {
  },
  data () {
    return {
      classType:[],
      submitLoading: false,
      formTitle: '',
      classTypeOptions:[],
      // 表单参数
      form: {
        id: null,

        name: null,

        roleType: 0,

        rankType: 0,

        sex: 0,

        deptId: null,

        phone: null,

        createBy: null,

        createTime: null,

        updateBy: null,

        updateTime: null,

        remark: null

      },
      // 1增加,2修改
      formType: 1,
      open: false,
      rules: {
        name: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        roleType: [
          { required: true, message: '角色不能为空', trigger: 'blur' }
        ],

        rankType: [
          { required: true, message: '职级不能为空', trigger: 'blur' }
        ],

        sex: [
          { required: true, message: '性别不能为空', trigger: 'blur' }
        ],

        phone: [
          { required: true, message: '电话不能为空', trigger: 'blur' }
        ]

      }
    }
  },
  filters: {
  },
  created () {
    this.getDicts('class_type').then(response => {
      this.classTypeOptions = response.data
    })
  },
  computed: {
  },
  watch: {
  },
  mounted () {
  },
  methods: {
    onClose () {
      this.open = false
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.formType = 1
      this.form = {
        id: null,

        name: null,

        roleType: 0,

        rankType: 0,

        sex: 0,

        deptId: null,

        phone: null,

        createBy: null,

        createTime: null,

        updateBy: null,

        updateTime: null,

        remark: null

      }
    },
    /** 新增按钮操作 */
    handleAdd (deptId) {
      this.reset()
      this.formType = 1
      this.open = true
      this.formTitle = '添加'
      this.form.deptId = deptId;
    },
    /** 修改按钮操作 */
    handleUpdate (row, ids,deptId) {
      this.reset()
      this.formType = 2
      const id = row ? row.id : ids
      getManagerUser(id).then(response => {
        this.form = response.data
        this.form.sex = response.data.sex + ""
        this.form.roleType = response.data.roleType ? response.data.roleType  + "" : null
        this.form.rankType = response.data.rankType ? response.data.rankType + "" : null
        this.open = true
        this.classType = response.data.classType.split(",");
        this.formTitle = '修改'
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      if(this.form.roleType == 1){
        if(this.classType.length > 0){
          this.form.classType = '',
          this.classType.forEach(item => {
            this.form.classType += item + ","
          });
          this.form.classType = this.form.classType.substr(0,this.form.classType.length - 1)
        }else{
          this.$message.error("班型不能为空！")
          return;
        }
      }
      

      if(this.form.phone.length != 11){
        this.$message.error("手机号填写错误！")
        return;
      }
      this.$refs.form.validate(valid => {
        if (valid) {
          this.submitLoading = true
          if (this.form.id !== undefined && this.form.id !== null) {
            updateManagerUser(this.form).then(response => {
              if(response.code == 500){
                this.$message.error('手机号重复！')
              }else{
                this.$message.success(
                  '修改成功！',
                  3
                )
                this.open = false
                this.$emit('ok')
              }
            }).finally(() => {
              this.submitLoading = false
            })
          } else {
            addManagerUser(this.form).then(response => {
              if(response.code == 500){
                this.$message.error('手机号重复！')
              }else{
                this.$message.success(
                  '新增成功!',
                  3
                )
                this.open = false
                this.$emit('ok')
              }
            }).finally(() => {
              this.submitLoading = false
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
